package com.jerry.huihome.module.find.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseActivity;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.entity.DesignerDetail;
import com.jerry.huihome.module.find.fragment.DesignerWorksFragment;
import com.jerry.huihome.module.find.fragment.DynamicFragment;
import com.jerry.huihome.module.find.fragment.MouthOfWordFragment;
import com.jerry.huihome.module.find_near_mine.db.HuihomeDBHelper;
import com.jerry.huihome.utils.OkHttpUtils;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by Administrator on 2016/5/11.
 */
public class DesignerDetailActivity extends BaseActivity {
    @BindView(R.id.tv_designers_detail_name)
    TextView tvDesignersDetailName;
    @BindView(R.id.iv_designer_head)
    RoundImageView ivDesignerHead;
    @BindView(R.id.tv_summary)
    TextView tvSummary;
    @BindView(R.id.tv_designer_name)
    TextView tvDesignerName;
    @BindView(R.id.rg_detail)
    RadioGroup rgDetail;
    @BindView(R.id.ratingBar_designer)
    RatingBar ratingBarDesigner;
    @BindView(R.id.tv_design_free)
    TextView tvDesignFree;
    @BindView(R.id.tv_design_theme)
    TextView tvDesignTheme;
    @BindView(R.id.tv_designers_work_years)
    TextView tvDesignersWorkYears;
    @BindView(R.id.tv_case_num)
    TextView tvCaseNum;
    @BindView(R.id.iv_share_designer)
    ImageView ivShareDesigner;
    @BindView(R.id.rb_koubei)
    RadioButton rbKoubei;
    @BindView(R.id.rb_dynamic)
    RadioButton rbDynamic;
    @BindView(R.id.rb_works)
    RadioButton rbWorks;
    @BindView(R.id.fl_select)
    FrameLayout flSelect;
    @BindView(R.id.iv_go_back)
    ImageView ivGoBack;
    @BindView(R.id.iv_attention)
    CheckBox ivAttention;
    private int designerid;
    private String url;
    private DesignerDetail designerDetail;
    private DisplayImageOptions options;
    private FragmentManager manager;
    private boolean isAttention = false;
    private HuihomeDBHelper dbHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_designer_detail;
    }

    @Override
    protected void initViews() {
        ShareSDK.initSDK(this);
        Intent intent = getIntent();
        designerid = intent.getIntExtra("designerId", 0);
        dbHelper = HuihomeDBHelper.getInstance(this);
    }

    @Override
    protected void loadData() {
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
        url = Urls.DESIGNER_DETAIL_URL + "designerid=" + designerid;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {

            @Override
            public void onResponse(String url, String data) {
                if (data != null) {
                    Gson gson = new Gson();
                    designerDetail = gson.fromJson(data, DesignerDetail.class);
                    setView();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
        //设置为浏览记录
        type = 1;
    }

    private void setView() {
        manager = getSupportFragmentManager();
        tvDesignersDetailName.setText("设计师-" + designerDetail.getBody().getDesignername());
        BaseApplication.getApplication().getImageLoader().displayImage(designerDetail.getBody().getAvatar(), ivDesignerHead, options);
        tvDesignerName.setText(designerDetail.getBody().getDesignername());
        ratingBarDesigner.setRating(designerDetail.getBody().getTotalScore());
        int feeLow = designerDetail.getBody().getFeeLow();
        int feeHigh = designerDetail.getBody().getFeeHigh();
        tvDesignFree.setText(feeLow + "-" + feeHigh + "/㎡");
        tvDesignTheme.setText(designerDetail.getBody().getDecoStyle());
        tvDesignersWorkYears.setText(designerDetail.getBody().getExpYear() + "年设计经验");
        tvCaseNum.setText(designerDetail.getBody().getCaseCount() + "");
        if (designerDetail.getBody().getResume() == null) {
            tvSummary.setText("暂无个人简历");
        } else {
            tvSummary.setText(designerDetail.getBody().getResume());
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("designerid", designerid + "");
        DynamicFragment fragment = BaseFragment.newInstance(DynamicFragment.class, map);
        manager.beginTransaction().replace(R.id.fl_select, fragment).commit();
        rgDetail.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_works:
                        Map<String, String> m = new HashMap<String, String>();
                        m.put("designerid", designerid + "");
                        DesignerWorksFragment worksFragment = BaseFragment.newInstance(DesignerWorksFragment.class, m);
                        manager.beginTransaction().replace(R.id.fl_select, worksFragment).commit();
                        break;
                    case R.id.rb_dynamic:
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("designerid", designerid + "");
                        DynamicFragment fragment = BaseFragment.newInstance(DynamicFragment.class, map);
                        manager.beginTransaction().replace(R.id.fl_select, fragment).commit();
                        break;
                    case R.id.rb_koubei:
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("designerid", designerid + "");
                        MouthOfWordFragment wordFragment = BaseFragment.newInstance(MouthOfWordFragment.class, params);
                        manager.beginTransaction().replace(R.id.fl_select, wordFragment).commit();
                        break;
                }

            }
        });
    }

    @OnClick(R.id.iv_share_designer)
    public void onClick() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(designerDetail.getBody().getDesignername() + "设计");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://www.tngou.net/info");
        // text是分享文本，所有平台都需要这个字段
        oks.setText(designerDetail.getBody().getDesignername() + "的装修理念、口碑评价、还有新动态哟");
        oks.setImageUrl(designerDetail.getBody().getAvatar());
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        //oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://www.tngou.net/info");

        // 启动分享GUI
        oks.show(this);
    }


    @OnClick(R.id.iv_go_back)
    public void onClickGoBack() {
        finish();
    }

    private int type = 0;
    @OnClick(R.id.iv_attention)
    public void onClickAttention(View view) {
        if(view !=null){
            type = 0;
        }
        if(designerDetail !=null){
            //赋值传递查询,判断是否收藏过,0代表收藏,1代表浏览
            if(dbHelper.isCollected(designerDetail.getBody().getDesignerid(),type)){
                Toast.makeText(this, "已经收藏过...", Toast.LENGTH_SHORT).show();
                return;
            }
            //设置type
            designerDetail.getBody().setType(type);
            //没有收藏就收藏,返回true;
            boolean isSuccess = dbHelper.saveAttention(designerDetail);
            //
            if(isSuccess && view !=null){
                Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
            }else {
                if(type ==0){
                    Toast.makeText(this, "收藏失败", Toast.LENGTH_SHORT).show();

                }
            }
        }
        //

        if (isAttention == false){
            Toast.makeText(this,"关注成功",Toast.LENGTH_LONG).show();
            isAttention = true;
        }else {
            Toast.makeText(this,"取消关注成功",Toast.LENGTH_LONG).show();
            isAttention = false;
        }



    }
}
